package son.hut.vn.app.network;

import java.io.IOException;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import son.hut.vn.app.infor.WebserviceConfig;
import son.hut.vn.app.utility.SmartLogUtility;

import android.content.Context;
import android.util.Log;


/**
 * AsyncHttpGet makes http post request based on AsyncTask
 * 
 * @author sonnv6207
 */
public class AsyncHttpPost extends AsyncHttpBase
{
    /**
     * Constructor
     * 
     * @param context
     * @param listener
     * @param parameters
     */
    public AsyncHttpPost(Context context, AsyncHttpResponseListener listener,
        List<NameValuePair> parameters)
    {
        super(context, listener, parameters);
    }

	/**
     * Constructor
     * 
     * @param context
     * @param process
     * @param parameters
     */
    public AsyncHttpPost(Context context, AsyncHttpResponseProcess process,
        List<NameValuePair> parameters)
    {
        super(context, process, parameters);
    }
    /*
     * com.seta.medtalkdroid.app.network.AsyncHttpBase#request(java.lang.String)
     */
    @Override
    protected String request(String url)
    {
        try
        {
            HttpParams params = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(params, WebserviceConfig.NETWORK_TIME_OUT);
            HttpConnectionParams.setSoTimeout(params, WebserviceConfig.NETWORK_TIME_OUT);
            
            HttpClient httpclient = createHttpClient(url, params);
            
            SmartLogUtility.log("client: ", httpclient.toString());
            
            
            HttpPost httppost = new HttpPost(url);
            SmartLogUtility.log("/////request--------------- : ", "/////request--------------- : " + httppost.toString());
            
            httppost.setEntity(new UrlEncodedFormEntity(parameters));

            SmartLogUtility.log("method request: ", httppost.getMethod());
            SmartLogUtility.log("request URI: ", httppost.getURI().toString());
            SmartLogUtility.log("request Line: ", httppost.getRequestLine().toString());
            try
            {
                response = httpclient.execute(httppost);
                statusCode = NETWORK_STATUS_OK;
                CAUSE_ERROR = "";
            }
            catch(java.net.SocketTimeoutException ex)
            {
                statusCode = NETWORK_STATUS_TIMEOUT;
                Log.d("HTTP_POST_ERROR", "Cause:" + ex.getMessage());
            }
            catch(javax.net.ssl.SSLException ex)
            {
                statusCode = NETWORK_STATUS_ERROR;
                ex.printStackTrace();
            }
            catch(org.apache.http.conn.ConnectTimeoutException timeoutEx){
                //TODO: Error while request time out
                statusCode = NETWORK_STATUS_TIMEOUT;
                timeoutEx.printStackTrace();
            }
            catch(ClientProtocolException clientEx){
                //ClientProtocolException - in case of an http protocol error
                statusCode = NETWORK_STATUS_ERROR;
                clientEx.printStackTrace();
            }
            catch(IOException ioEx){
              //IOException - in case of a problem or the connection was aborted
                statusCode = NETWORK_STATUS_ERROR;
                ioEx.printStackTrace();
            }
            
            Log.d("STATUSCODE","HttpPost:" + statusCode);
        }
        catch (Exception e)
        {
            statusCode = NETWORK_EXCEPTION;
            CAUSE_ERROR = e.getMessage();
            Log.d("HTTP_POST_ERROR",e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
}
